package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 方法可以返回ModelAndView相关的：没有使用@ResponseBody，返回值写String或ModelAndView
 * 也可以返回Json：@ResponseBody
 * @author stone
 * @date 2022/11/17 17:02
 */
//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice
public class ExceptionControllerAdvice {

    /*@ExceptionHandler(ArithmeticException.class)
    //@ResponseBody
    public BaseRespVo method1(ArithmeticException exception) {
        return BaseRespVo.fail(exception.getMessage());
    }
    @ExceptionHandler(NumberFormatException.class)
    public BaseRespVo method2(NumberFormatException exception) {
        return BaseRespVo.fail(exception.getMessage());
    }*/
    // 如果不同的异常，异常处理的业务逻辑不同的话，放到不同方法中；
    // 如果处理的逻辑相同，也可以放到同一个方法中
    @ExceptionHandler({NumberFormatException.class,ArithmeticException.class})
    public BaseRespVo method2(Exception exception) {
        return BaseRespVo.fail("ExceptionHandler: " + exception.getMessage());
    }
}
